package cn.kgc.vueebook2.service;


import cn.kgc.vueebook2.mapper.EntryMapper;
import cn.kgc.vueebook2.pojo.Entry;
import cn.kgc.vueebook2.pojo.EntryExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-05-18 16:38
 */
@Service
public class EntryServiceImpl implements EntryService {

    @Resource
    EntryMapper entryMapper;

    @Override
    public PageInfo<Entry> selectAllEntry(Integer pageSize, Integer pageNum, Integer categoryId) {
        PageHelper.startPage(pageNum, pageSize);

        EntryExample example = new EntryExample();
        EntryExample.Criteria criteria = example.createCriteria();

        if(categoryId==0){
            List<Entry> entries = entryMapper.selectByExample(null);
            PageInfo<Entry> pageInfo=new PageInfo<>(entries);
            return pageInfo;
        }else {
            criteria.andCategoryidEqualTo(categoryId);
            List<Entry> entries = entryMapper.selectByExample(example);
            PageInfo<Entry> pageInfo=new PageInfo<>(entries);
            return pageInfo;
        }
    }

    @Override
    public Integer addEntry(Entry entry) {
      return  entryMapper.insert(entry);
    }

    @Override
    public Integer deleteEntry(Integer id) {
     return   entryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Entry selectById(Integer id) {

        return entryMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer update(Entry entry) {
      return  entryMapper.updateByPrimaryKey(entry);
    }
}
